/*
 * 测试真实的JavaScript解析器
 */

#include <stdio.h>
#include <dlfcn.h>
#include <string.h>
#include "../c-deps/tree_sitter.h"

int main() {
    printf("=== 真实JavaScript解析器测试 ===\n");
    
    // 1. 加载真实的JavaScript解析器动态库
    void *handle = dlopen("./tree-sitter-javascript.so", RTLD_LAZY);
    if (!handle) {
        printf("❌ 无法加载JavaScript解析器: %s\n", dlerror());
        return 1;
    }
    
    printf("✅ JavaScript解析器动态库加载成功\n");
    
    // 2. 获取语言函数
    const TSLanguage *(*tree_sitter_javascript)(void) = dlsym(handle, "tree_sitter_javascript");
    if (!tree_sitter_javascript) {
        printf("❌ 无法找到tree_sitter_javascript函数: %s\n", dlerror());
        dlclose(handle);
        return 1;
    }
    
    printf("✅ tree_sitter_javascript函数获取成功\n");
    
    // 3. 获取JavaScript语言对象
    const TSLanguage *js_language = tree_sitter_javascript();
    if (!js_language) {
        printf("❌ 无法获取JavaScript语言对象\n");
        dlclose(handle);
        return 1;
    }
    
    printf("✅ JavaScript语言对象创建成功\n");
    printf("   语言版本: %u\n", ts_language_version(js_language));
    printf("   符号数量: %u\n", ts_language_symbol_count(js_language));
    printf("   字段数量: %u\n", ts_language_field_count(js_language));
    
    // 4. 创建解析器
    TSParser *parser = ts_parser_new();
    if (!parser) {
        printf("❌ 无法创建解析器\n");
        dlclose(handle);
        return 1;
    }
    
    printf("✅ 解析器创建成功\n");
    
    // 5. 设置语言
    if (!ts_parser_set_language(parser, js_language)) {
        printf("❌ 无法设置JavaScript语言\n");
        ts_parser_delete(parser);
        dlclose(handle);
        return 1;
    }
    
    printf("✅ JavaScript语言设置成功\n");
    
    // 6. 解析真实的JavaScript代码
    const char *js_code = 
        "function hello(name) {\n"
        "    if (name) {\n"
        "        console.log('Hello, ' + name);\n"
        "    } else {\n"
        "        console.log('Hello, World!');\n"
        "    }\n"
        "}\n"
        "\n"
        "class Greeter {\n"
        "    constructor(greeting) {\n"
        "        this.greeting = greeting;\n"
        "    }\n"
        "    \n"
        "    greet(name) {\n"
        "        return this.greeting + ', ' + name;\n"
        "    }\n"
        "}\n"
        "\n"
        "const greeter = new Greeter('Hello');\n"
        "hello('Tree-sitter');\n"
        "console.log(greeter.greet('JavaScript'));\n";
    
    printf("\n📄 正在解析JavaScript代码:\n");
    printf("代码长度: %zu 字符\n", strlen(js_code));
    
    TSTree *tree = ts_parser_parse_string(parser, NULL, js_code, strlen(js_code));
    if (!tree) {
        printf("❌ JavaScript代码解析失败\n");
        ts_parser_delete(parser);
        dlclose(handle);
        return 1;
    }
    
    printf("✅ JavaScript代码解析成功！\n");
    
    // 7. 获取根节点并分析AST
    TSNode root_node = ts_tree_root_node(tree);
    printf("\n🌳 AST分析结果:\n");
    printf("   根节点类型: %s\n", ts_node_type(root_node));
    printf("   子节点数量: %u\n", ts_node_child_count(root_node));
    printf("   命名子节点数量: %u\n", ts_node_named_child_count(root_node));
    printf("   开始位置: (%u, %u)\n", 
           ts_node_start_point(root_node).row, 
           ts_node_start_point(root_node).column);
    printf("   结束位置: (%u, %u)\n", 
           ts_node_end_point(root_node).row, 
           ts_node_end_point(root_node).column);
    
    // 8. 遍历顶层节点
    printf("\n📋 顶层节点列表:\n");
    uint32_t child_count = ts_node_named_child_count(root_node);
    for (uint32_t i = 0; i < child_count && i < 10; i++) {
        TSNode child = ts_node_named_child(root_node, i);
        printf("   %u. %s [%u:%u - %u:%u]\n", 
               i + 1,
               ts_node_type(child),
               ts_node_start_point(child).row,
               ts_node_start_point(child).column,
               ts_node_end_point(child).row,
               ts_node_end_point(child).column);
    }
    
    // 9. 清理资源
    ts_tree_delete(tree);
    ts_parser_delete(parser);
    dlclose(handle);
    
    printf("\n🎉 真实JavaScript解析器测试完成！\n");
    printf("✅ 所有功能正常工作\n");
    
    return 0;
}
